package com.mall.web.control.eshop;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.biz.eshop.EshopTradeBizImpl;
import com.mall.biz.eshop.TransferAuditBizImpl;
import com.mall.biz.trade.TradeBizImpl;
import com.mall.biz.user.VerifyCodeBizImpl;
import com.mall.client.commkey.CommKeyService;
import com.mall.client.eshop.EshopTradeService;
import com.mall.client.eshop.SaleEshopService;
import com.mall.client.eshop.TransferAuditService;
import com.mall.client.trade.TradeService;
import com.mall.client.user.SessService;
import com.mall.client.user.UserExtendedService;
import com.mall.client.user.UserService;
import com.mall.common.util.bit.PropertiesConfigure;
import com.mall.constant.UserConstant;
import com.mall.pojo.EshopTrade;
import com.mall.pojo.SaleEshop;
import com.mall.pojo.Trade;
import com.mall.pojo.TradeExample;
import com.mall.pojo.User;
import com.mall.pojo.UserExtended;
import com.mall.query.TradeRecordQuery;
import com.mall.vo.trade.TradeVO;
import com.mall.web.annotation.AdminSecurity;
import com.mall.web.annotation.EShopUserSecurity;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.MaiGeDianWebUtil;
import com.mall.web.util.WebUtil;
import com.mall.web.vo.ResponseEntity;

@Controller
@RequestMapping("/eshop/trade/")
/**
 * 订单操作的controller
 * @author Administrator
 *
 */
public class EshopTradeController {
	private Logger log = LoggerFactory.getLogger(EshopTradeController.class);

	@Autowired
	private UserExtendedService userExtendedService;
	@Autowired
	private UserService userService;
	@Autowired
	private VerifyCodeBizImpl verifyCodeBiz;
	@Autowired
	private PropertiesConfigure propertiesConfigure;
	@Autowired
	private CommKeyService commKeyService;
	@Autowired
	private SessService sessService;
	@Autowired
	private TransferAuditService transferAuditService;
	@Autowired
	private TransferAuditBizImpl transferAuditBizImpl;
	@Autowired
	private SaleEshopService saleEshopService;
	@Autowired
	private EshopTradeService eshopTradeService;
	@Autowired
	private TradeService tradeService;
	@Autowired
	private EshopTradeBizImpl eshopTradeBizImpl;
	@Autowired
	private TradeBizImpl tradeBizImpl;
	
	
	public static void main(String[] args) {
		Long totalPrice = 10000l;
		Long bePayMoney =totalPrice - (long) (totalPrice * MaiGeDianWebUtil.dingjinScale);
		System.out.println(bePayMoney);
	}
	
	
	
	@RequestMapping("backTradeManage")
	@EShopUserSecurity(isPc=true)
	@AdminSecurity(userType = UserConstant.TYPE_MANAGE)
	public String backTradeManage(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap){
		User user = WebUtil.getCurrentUser(request);
		modelMap.put("user", user);
		return "eshop/back/backTradeManage";
	}
	
	
	
	/**
	 * 用户未付款的订单
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("eshopTradeList")
	@EShopUserSecurity(isPc=true)
	@AdminSecurity(userType = UserConstant.TYPE_MANAGE)
	public String eshopTradeList(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap){
		User user = WebUtil.getCurrentUser(request);
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		List<EshopTrade> eshopTradeList = this.eshopTradeBizImpl.getEshopTradeListByUserIdAndOnlyKey(user.getId(), onlyKey, 0, null);
		List<TradeVO> tradeVOList = new ArrayList<TradeVO>();
		TradeVO tradeVO =null;
		if(eshopTradeList != null && eshopTradeList.size() > 0){
			for(EshopTrade eshopTrade : eshopTradeList){
				tradeVO = new TradeVO();
				tradeVO.setEshopTrade(eshopTrade);
				tradeVO.setSaleEshop(this.saleEshopService.getSaleEshopBySaleEshopId(eshopTrade.getSaleEshopId()));
				Long totlePrice = eshopTrade.getPrice()+eshopTrade.getJishuPrice()+eshopTrade.getShouxuPrice()+eshopTrade.getXiaobaoPrice();
				tradeVO.setOrPrice(totlePrice);
				tradeVOList.add(tradeVO);
			}
		}
		modelMap.put("tradeVOList", tradeVOList);
		modelMap.put("user", user);
		return "eshop/trade/eshopTradeList";
	}
	
	/**
	 * 用户删除未付款订单
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("payTradeList")
	public String payTradeList(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap){
		User user = WebUtil.getCurrentUser(request);
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		Integer status = HttpUtil.getIntegerValueFromRequest(request, "status");
		TradeRecordQuery tradeRecordQuery = new TradeRecordQuery();
		tradeRecordQuery.setOnlyKey(onlyKey);
		tradeRecordQuery.setUserId(user.getId());
		tradeRecordQuery.setStatus(status);
		tradeRecordQuery.setType(89);
		tradeRecordQuery.setOrderby("id desc");
		tradeRecordQuery.setPage(null);
		TradeExample tradeExample = this.tradeBizImpl.getTradeExampleByQuery(tradeRecordQuery);
		List<Trade> tradeList = this.tradeService.getTradeMessListByExample(tradeExample);
		
		List<TradeVO> tradeVOList = new ArrayList<TradeVO>();
		TradeVO tradeVO =null;
		if(tradeList != null && tradeList.size() > 0){
			for(Trade trade : tradeList){
				tradeVO = new TradeVO();
				EshopTrade eshopTrade = this.eshopTradeService.getEshopTradeByEshopTradeId(trade.getParentId());
				tradeVO.setTrade(trade);
				tradeVO.setEshopTrade(eshopTrade);
				tradeVO.setSaleEshop(this.saleEshopService.getSaleEshopBySaleEshopId(eshopTrade.getSaleEshopId()));
				tradeVO.setOrPrice(eshopTrade.getPrice()+eshopTrade.getJishuPrice()+eshopTrade.getShouxuPrice()+eshopTrade.getXiaobaoPrice());
				tradeVOList.add(tradeVO);
			}
		}
		modelMap.put("tradeVOList", tradeVOList);
		modelMap.put("user", user);
		return "eshop/trade/payTradeList";
	} 
	
	/**
	 * 用户删除未付款订单
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("deleteEshopTrade")
	@ResponseBody
	public ResponseEntity<String> deleteEshopTrade(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap){
		ResponseEntity<String> responseEntity = new ResponseEntity<String>();
		Long eshopTradeId = HttpUtil.getLongValueFromRequest(request, "eshopTradeId");
		EshopTrade eshopTrade = this.eshopTradeService.getEshopTradeByEshopTradeId(eshopTradeId);
		if(eshopTrade == null){
			responseEntity.initErrorResponseEntity(null, "订单不存在");
			return responseEntity;
		}
		eshopTrade.setStatus(-1);
		this.eshopTradeService.modifyEshopTradeByEshopTrade(eshopTrade);
		responseEntity.initSuccResponseEntity(null, "删除成功");
		return responseEntity;
	}
	
	/**
	 * 用户删除已完成订单
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("deleteTrade")
	@ResponseBody
	public ResponseEntity<String> deleteTrade(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap){
		ResponseEntity<String> responseEntity = new ResponseEntity<String>();
		Long tradeId = HttpUtil.getLongValueFromRequest(request, "tradeId");
		Trade trade = this.tradeService.getTradeByOrderId(tradeId);
		if(trade == null){
			responseEntity.initErrorResponseEntity(null, "订单不存在");
			return responseEntity;
		}
		trade.setStatus(-1);
		this.tradeService.updateTradeByTrade(trade);
		responseEntity.initSuccResponseEntity(null, "删除成功");
		return responseEntity;
	}
	
	/**
	 * 用户支付尾款
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("payLast")
	@ResponseBody
	public ResponseEntity<String> payLast(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap){
		ResponseEntity<String> responseEntity = new ResponseEntity<String>();
		Long tradeId = HttpUtil.getLongValueFromRequest(request, "tradeId");
		Trade trade = this.tradeService.getTradeByOrderId(tradeId);
		if(trade == null || trade.getStatus() != 1){
			responseEntity.initErrorResponseEntity(null, "订单不存在");
			return responseEntity;
		}
		
		User user = WebUtil.getCurrentUser(request);
		EshopTrade eshopTrade = this.eshopTradeService.getEshopTradeByEshopTradeId(trade.getParentId());
		Long totalPrice = eshopTrade.getPrice()+eshopTrade.getJishuPrice()+eshopTrade.getShouxuPrice()+eshopTrade.getXiaobaoPrice();
		Long bePayMoney = totalPrice - eshopTrade.getDingjinPrice();
		UserExtended userExtended = this.userExtendedService.getUserExtendByUserId(user.getId());
		if(userExtended == null || userExtended.getBalance() == null || userExtended.getBalance() < bePayMoney){
			responseEntity.setCode("-300");
			responseEntity.setData(null);
			responseEntity.setMessage("余额不足请充值！");
			return responseEntity;
		}
		userExtended.setBalance(userExtended.getBalance() - bePayMoney);
		this.userExtendedService.updateExtendedByPrimaryKey(userExtended);
		trade.setStatus(2);
		this.tradeService.updateTradeByTrade(trade);
		responseEntity.initSuccResponseEntity(null, "支付成功");
		return responseEntity;
	}
	
	/**
	 * 用户买店下单页面
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("eshopTradeAction")
	@EShopUserSecurity(isPc=true)
	public String eshopTradeAction(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap){
		User user = WebUtil.getCurrentUser(request);
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		Long saleEshopId = HttpUtil.getLongValueFromRequest(request, "saleEshopId");
		
		SaleEshop saleEshop = this.saleEshopService.getSaleEshopBySaleEshopId(saleEshopId);
		if(saleEshop == null){
			return null;
		}
		EshopTrade eshopTrade = new EshopTrade();
		if(saleEshop.getTechnologyMoney() != null){
			eshopTrade.setJishuPrice((new BigDecimal(saleEshop.getTechnologyMoney())).multiply(new BigDecimal(100)).longValue());
		}
		if(saleEshop.getBondMoney() != null){
			eshopTrade.setXiaobaoPrice((new BigDecimal(saleEshop.getBondMoney())).multiply(new BigDecimal(100)).longValue());
		}
		eshopTrade.setDingjinPrice((new BigDecimal(saleEshop.getPrice())).multiply(new BigDecimal(100)).multiply(new BigDecimal(MaiGeDianWebUtil.dingjinScale)).longValue());
		eshopTrade.setShouxuPrice((new BigDecimal(saleEshop.getPrice())).multiply(new BigDecimal(100)).multiply(new BigDecimal(MaiGeDianWebUtil.shouxuScale)).longValue());
		eshopTrade.setBuyUserId(user.getId());
		eshopTrade.setCommOnlyKey(onlyKey);
		eshopTrade.setSaleEshopId(saleEshopId);
		eshopTrade.setStatus(0);
		eshopTrade.setTradeNo(tradeService.generateOrderNo());
		eshopTrade.setType(0);
		eshopTrade.setPrice((new BigDecimal(saleEshop.getPrice())).multiply(new BigDecimal(100)).longValue());
		int insertSucess = this.eshopTradeService.insertEshopTradeByEshopTrade(eshopTrade);

		if (insertSucess == 1) {
			return "redirect:/eshop/trade/eshopTradeDetail.htm?eshopTradeId="+eshopTrade.getId();
		}
		return null;
	}
	
	/**
	 * 用户买店下单后的eshopTrade详情
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("eshopTradeDetail")
	@EShopUserSecurity(isPc=true)
	public String eshopTradeDetail(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap){
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		User user = WebUtil.getCurrentUser(request);
		Long eshopTradeId = HttpUtil.getLongValueFromRequest(request, "eshopTradeId");
		
		EshopTrade eshopTrade = this.eshopTradeService.getEshopTradeByEshopTradeId(eshopTradeId);
		if(eshopTrade == null){
			return null;
		}
		SaleEshop saleEshop = this.saleEshopService.getSaleEshopBySaleEshopId(eshopTrade.getSaleEshopId());
		UserExtended userExtended = this.userExtendedService.getUserExtendByUserId(user.getId());
		if(userExtended == null){
			userExtended = new UserExtended();
			userExtended.setUserId(user.getId());
			userExtended.setBalance(0l);
			userExtended.setBond(0l);
		}
		Long totlePrice = eshopTrade.getPrice()+eshopTrade.getJishuPrice()+eshopTrade.getShouxuPrice()+eshopTrade.getXiaobaoPrice();
		modelMap.put("user", user);
		modelMap.put("totlePrice", totlePrice);
		modelMap.put("eshopTrade", eshopTrade);
		modelMap.put("saleEshop", saleEshop);
		modelMap.put("userExtended", userExtended);
		return "eshop/trade/eshopTradeDetail";
	}
	
	
	
}
